package indi.kider.server.qiniu.service;

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pers.vankid.common.constant.DateTime;

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

@Service
@Slf4j
public class QiniuUpTokenServiceImpl implements QiniuUpTokenService {

    private static String UPTOKEN;
    private static long EXP_SECONDS = 3700;

    public static String accessKey = "GMW-Ebd5-4DLxjGXgaOb5BSwak9l_Iw3c6ApzfvW";
    public static String secretKey = "ahaO2XPRmvnCXkebOkSUiglPA0WGVce3JyjfgjUz";

    static final String bucket = "woniu";

    @Override
    public String getUpToken() {
        return UPTOKEN;
    }

    @Override
    public void updateToken() {
        Auth auth = Auth.create(accessKey, secretKey);
        StringMap putPolicy = new StringMap();

//		putPolicy.put("callbackUrl", "http://api.example.com/qiniu/upload/callback");
//		putPolicy.put("callbackBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
//		putPolicy.put("callbackBodyType", "application/json");

        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
        UPTOKEN = auth.uploadToken(bucket, null, EXP_SECONDS, putPolicy);
    }

}
